package com.lesso.zgs.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.zgs.sunflower.domain.repository.AttendanceRuleRepository;
import com.lesso.zgs.sunflower.infra.assembler.AttendanceRuleAssembler;
import com.lesso.zgs.sunflower.infra.mapper.AttendanceRuleMapper;
import com.lesso.zgs.sunflower.model.AttendanceRule;
import com.lesso.zgs.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.zgs.sunflower.pojo.vo.AttendanceRuleVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * AttendanceRuleRepositoryImpl
 *
 * @author zgs
 * @since 2025-07-03
 */
@Repository
public class AttendanceRuleRepositoryImpl implements AttendanceRuleRepository {
    private final AttendanceRuleMapper attendanceRuleMapper;

    public AttendanceRuleRepositoryImpl(AttendanceRuleMapper attendanceRuleMapper) {
        this.attendanceRuleMapper = attendanceRuleMapper;
    }

    @Override
    public Integer save(AttendanceRule attendanceRule) {
        attendanceRuleMapper.insert(attendanceRule);
        return Math.toIntExact(attendanceRule.getId());
    }

    @Override
    public AttendanceRule getById(Long id) {
        return attendanceRuleMapper.selectById(id);
    }

    @Override
    public Integer update(AttendanceRule attendanceRule) {
        return attendanceRuleMapper.updateById(attendanceRule);
    }

    @Override
    public Integer delete(Long id) {
        return attendanceRuleMapper.deleteById(id);
    }

    @Override
    public Page<AttendanceRuleVO> getPageList(AttendanceRuleQueryDTO dto) {
        if (dto == null) {
            throw new IllegalArgumentException("AttendanceRuleQueryDTO is null");
        }
        Page<AttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<AttendanceRule> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getRuleCode()), "rule_code", dto.getRuleCode());
        wrapper.like(StringUtils.hasText(dto.getRuleName()), "rule_name", dto.getRuleName());
        List<AttendanceRule> attendanceRules = attendanceRuleMapper.selectPage(page, wrapper);
        return page.setRecords(AttendanceRuleAssembler.INSTANCE.entityListToVOList(attendanceRules));
    }
} 